package com.company.clientscard.main;

import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.event.shared.UmbrellaException;
import com.google.gwt.user.client.Window;

public class CustomUncaughtExceptionHandler implements UncaughtExceptionHandler {

	@Override
	public void onUncaughtException(Throwable e) {
		// Get rid of UmbrellaException
		Throwable exceptionToDisplay = getExceptionToDisplay(e);
		// Replace with your favorite message dialog, e.g. GXT's MessageBox
		Window.alert(exceptionToDisplay.getMessage());
	}

	private Throwable getExceptionToDisplay(Throwable throwable) {
		Throwable result = throwable;
		if (throwable instanceof UmbrellaException
				&& ((UmbrellaException) throwable).getCauses().size() == 1) {
			result = ((UmbrellaException) throwable).getCauses().iterator()
					.next();

		}

		return result;
	}
}
